package com.xxcxcxcx.jobsystem.mixin.job.Herdsman;

import com.xxcxcxcx.jobsystem.PlayerSaveData;
import com.xxcxcxcx.jobsystem.common.JobsClass;
import com.xxcxcxcx.jobsystem.common.capability.JobCapabilityProvider;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.OwnableEntity;
import net.minecraft.world.entity.TamableAnimal;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.Wolf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Wolf.class)
public abstract class WolfMixin extends Animal implements OwnableEntity {
    @Shadow public abstract void setTame(boolean p_30443_);

    protected WolfMixin(EntityType<? extends TamableAnimal> p_21803_, Level p_21804_) {
        super(p_21803_, p_21804_);
    }
    public boolean Fin=false;
    @Inject(method = "setTame",at = @At(value = "HEAD"),cancellable = true)
    private void WolfAttributes(CallbackInfo ci) {
        JobCapabilityProvider.getPlayerData((Player) this.getOwner()).ifPresent(playerSaveData -> {
            if(playerSaveData != null) {
                if(this.getOwner() instanceof ServerPlayer) {
                    Fin = true;
                    int Joblevel = playerSaveData.getLevel(JobsClass.Herdsman);
                    if(Joblevel >= 7) {
                        this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(20.0D + 5.0 * (Joblevel - 6));
                        this.setHealth(20.0f + 5.0f * (Joblevel - 6));
                        this.getAttribute(Attributes.ATTACK_DAMAGE).setBaseValue(4.0D + 0.5 * (Joblevel - 6));
                        this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.3F + 0.05f * (Joblevel - 6));
                    } else {
                        this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(20.0D);
                        this.setHealth(20.0F);
                    }
                }
                ci.cancel();
            }
        });
    }
    @Inject(method = "tick",at = @At(value = "HEAD"))
    private void tickChange(CallbackInfo ci) {
        if(this.getOwner()!=null && !Fin)setTame(true);
    }
}
